<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>结算单 - {{ purchaser_name }}</title>
    <style>
        /* CSS Reset and Basic Styles */
        body {
            font-family: 'SimSun', 'Arial', 'sans-serif'; /* Preferred Chinese font, fallback to Arial */
            margin: 0;
            padding: 40px;
            color: #333;
            line-height: 1.6;
            font-size: 10pt;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            border: 1px solid #eee;
            padding: 30px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
        }

        /* Header */
        .header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 30px;
            border-bottom: 2px solid #000;
            padding-bottom: 15px;
        }
        .header h1 {
            font-size: 24pt;
            color: #000;
            margin: 0;
            font-weight: bold;
        }
        .company-info {
            text-align: right;
            font-size: 9pt;
        }
        .company-info img {
            max-width: 150px;
            height: auto;
            margin-bottom: 10px;
        }
        .statement-meta {
            margin-top: 20px;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            font-size: 10pt;
        }
        .statement-meta div span {
            font-weight: bold;
        }

        /* Client Info */
        .client-info {
            margin-bottom: 20px;
            padding: 10px 0;
            border-bottom: 1px dashed #ccc;
        }
        .client-info p {
            margin: 5px 0;
        }
        .client-info strong {
            font-size: 11pt;
            color: #000;
        }

        /* Order Details Table */
        .order-section {
            margin-bottom: 25px;
            border: 1px solid #ddd;
            padding: 15px;
            background-color: #f9f9f9;
        }
        .order-header {
            font-weight: bold;
            font-size: 11pt;
            margin-bottom: 10px;
            color: #555;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 15px;
        }
        table thead th {
            background-color: #e8e8e8;
            border: 1px solid #ccc;
            padding: 8px;
            text-align: left;
            font-weight: bold;
        }
        table tbody td {
            border: 1px solid #eee;
            padding: 8px;
            vertical-align: top;
        }
        table tfoot td {
            border: 1px solid #ccc;
            padding: 8px;
            font-weight: bold;
            text-align: right;
        }
        table tfoot td:first-child {
            text-align: left;
            border: none;
        }
        .text-right {
            text-align: right;
        }

        /* Summary */
        .summary {
            margin-top: 30px;
            text-align: right;
            font-size: 12pt;
            font-weight: bold;
        }
        .summary p {
            margin: 5px 0;
        }
        .total-due {
            font-size: 16pt;
            color: #d9534f; /* A prominent color for the total */
            border-top: 2px solid #d9534f;
            padding-top: 10px;
        }

        /* Footer */
        .footer {
            margin-top: 50px;
            border-top: 1px solid #ccc;
            padding-top: 20px;
            font-size: 9pt;
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
        }
        .bank-info, .contact-info {
            width: 48%;
            vertical-align: top;
        }
        .official-seal {
            width: 150px;
            height: 80px;
            border: 1px dashed #ccc;
            text-align: center;
            line-height: 80px;
            color: #aaa;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div>
                <h1>结算单</h1>
            </div>
            <div class="company-info">
                <strong>您的公司名称</strong><br>
                地址：[您的公司地址]<br>
                电话：[您的公司电话]<br>
                邮箱：[您的公司邮箱]
            </div>
        </div>

        <div class="statement-meta">
            <div>结算单编号: <span>{{ statement_id }}</span></div>
            <div>开具日期: <span>{{ current_date }}</span></div>
        </div>

        <div class="client-info">
            <p><strong>致: {{ purchaser_name }}</strong></p>
            <p>感谢您选择我们的服务。</p>
            <p>以下是您公司未结算的采购明细，请查阅并及时完成结算。</p>
        </div>

       {% set ns = namespace(grand_total_unsettled_amount=0.0) %}

        {% if unsettled_orders %}
            {% for order in unsettled_orders %}
                <div class="order-section">
                    <div class="order-header">
                        订单号: {{ order.order_info.order_id }} &nbsp;&nbsp;|&nbsp;&nbsp; 下单时间: {{ order.order_info.order_time }} &nbsp;&nbsp;|&nbsp;&nbsp; 订单金额: ¥{{ "%.2f"|format(order.order_info.total_amount) }}
                    </div>
                    <table>
                        <thead>
                            <tr>
                                <th>商品名称</th>
                                <th>商品类型</th>
                                <th>规格</th>
                                <th class="text-right">数量</th>
                                <th class="text-right">单价</th>
                                <th class="text-right">金额</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for detail in order.order_details %}
                            <tr>
                                <td>{{ detail.product_info.product_name }}</td>
                                <td>{{ detail.product_info.product_type }}</td>
                                <td>{{ detail.product_info.specification }}</td>
                                <td class="text-right">{{ detail.quantity }}</td>
                                <td class="text-right">¥{{ "%.2f"|format(detail.price_at_order) }}</td>
                                <td class="text-right">¥{{ "%.2f"|format(detail.amount) }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="5">订单小计</td>
                                <td class="text-right">¥{{ "%.2f"|format(order.order_info.total_amount) }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
                {% set ns.grand_total_unsettled_amount = ns.grand_total_unsettled_amount + order.order_info.total_amount %}
            {% endfor %}
        {% else %}
            <p>此采购方目前没有未结算的订单。</p>
        {% endif %}

        <div class="summary">
            <p>未结算总金额:</p>
            <p class="total-due">¥{{ "%.2f"|format(ns.grand_total_unsettled_amount) }}</p>
        </div>

        <div class="footer">
            <div class="bank-info">
                <strong>银行账户信息</strong><br>
                开户行: [您的开户银行名称]<br>
                账户名称: [您的公司名称]<br>
                银行账号: [您的银行账号]<br>
                SWIFT/BIC: [如适用]
            </div>
            <div class="contact-info">
                <strong>联系方式</strong><br>
                联系人: [您的联系人姓名]<br>
                电话: [您的联系电话]<br>
                网址: [您的公司网址]
            </div>
            <div class="official-seal">
                （公司盖章处）
            </div>
        </div>
    </div>
</body>
</html>